﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using Ranorex.Core;
using Ranorex.Robot.Domain;

namespace Ranorex.Robot
{
    public class Keywords
    {

    	private AppUnderTest _app = AppUnderTest.getInstance();
    	
        #region Application

        /// <summary>
        /// Starts an exe windows application with cmd line arguments
        /// </summary>
        public void application_open_withargs(String exepath, String arguments)
        {
        	_app.Start(exepath,arguments);
        }

        /// <summary>
        /// Starts an exe windows application without cmd line arguments
        /// </summary>
        public void application_open(String exepath)
        {
        	_app.Start(exepath);
        }

        /// <summary>
        /// Kills the application
        /// </summary>
        public void application_kill()
        {
        	_app.Kill();
        }
        
        /// <summary>
        /// Activates and gives focus to the main window of the application
        /// </summary>
        public void application_set_focus_main_window()
        {
        	_app.ActivateMainWindow();
        }
        
        /// <summary>
        /// Activates and gives focus to a child window of the application with Title matching regular expression
        /// </summary>
        public void application_set_focus_child_window(String regex)
        {
        	_app.ActivateChildWindow(regex);
        }
        
        /// <summary>
        /// Checks if the application has a child window with a Title matching the regular expression.
        /// Returns True/False
        /// </summary>
        public String application_has_child_window(String regex)
        {
        	return _app.HasChildWindow(regex).ToString();
        }
        
        /// <summary>
        /// Waits until a child window is found with a Title matching the regular expression.
        /// If no child window match found within the specified seconds, the keyword fails
        /// </summary>
        public void application_wait_for_child_window(String regex, String seconds)
        {
        	if (String.IsNullOrEmpty(seconds)) throw new KeywordException("Invalid seconds parameter");
        	int secs = int.Parse(seconds);
        	_app.WaitForChildWindow(regex,secs);
        }
        
        /// <summary>
        /// Checks if the application has a dialog window and returns True/False
        /// </summary>
        public String application_has_dialog_window()
        {
        	return _app.HasDialogWindow().ToString();
        }
        
        /// <summary>
        /// Waits until the application displays a dialog window. If none found within specified seconds keyword fails
        /// </summary>
        public void application_wait_for_dialog_window(String seconds)
        {
        	if (String.IsNullOrEmpty(seconds)) throw new KeywordException("Invalid seconds parameter");
        	int secs = int.Parse(seconds);
        	_app.WaitForDialogWindow(secs);
        }

        /// <summary>
        /// Activates and sets focus to application dialog window.
        /// Keyword fails if application has no dialog window
        /// </summary>
        public void application_set_focus_dialog_window()
        {
        	_app.ActivateDialogWindow();
        }
        
        /// <summary>
        /// Waits until the application has no dialog windows.
        /// If the application has a dialog window after seconds, the keyword fails
        /// </summary>
        /// <param name="seconds"></param>
        public void application_wait_for_no_dialog_window(String seconds)
        {
        	if (String.IsNullOrEmpty(seconds)) throw new KeywordException("Invalid seconds parameter");
        	int secs = int.Parse(seconds);
        	_app.WaitForNoDialogWindow(secs);
        }
        
        /// <summary>
        /// Closes the main window of the application.
        /// Does not wait for the application process to terminate.
        /// </summary>
        public void application_close_main_window()
        {
        	_app.CloseMainWindow();
        }
        
        /// <summary>
        /// Waits for the application process to terminate.
        /// If the application process is still running after seconds, the keyword fails
        /// </summary>
        /// <param name="seconds"></param>
        public void application_wait_for_terminate(String seconds)
        {
        	if (String.IsNullOrEmpty(seconds)) throw new KeywordException("Invalid seconds parameter");
        	int secs = int.Parse(seconds);
        	_app.WaitForTerminate(secs);
        }
        
        
        #endregion

        #region Element
        
        /// <summary>
        /// Sets focus to the control element
        /// </summary>
        public void element_set_focus(String xpath)
        {
        	Ranorex.Core.Element el = _app.GetElement(xpath);
        	el.Focus();
        }
        
        /// <summary>
        /// Checks if the element has focus and returns True/False
        /// </summary>
        public String element_has_focus(String xpath)
        {
        	Ranorex.Core.Element el = _app.GetElement(xpath);
        	return el.HasFocus.ToString();
        }
        
        
        #endregion
        
        #region Button

        /// <summary>
        /// Clicks button with left mouse
        /// </summary>
        public void button_click(String xpath)
        {
            Ranorex.Button el = new Ranorex.Button(_app.GetElement(xpath));
            if ((!el.Visible) || (!el.Enabled)) throw new KeywordException("Button is not visible or not enabled");
            el.Click();
        }
        
        /// <summary>
        /// Gets the text displayed on the button
        /// </summary>
        public String button_get_text(String xpath)
        {
        	Ranorex.Button el = new Ranorex.Button(_app.GetElement(xpath));
        	return el.Text;
        }
        
        #endregion

        #region TextBox

        /// <summary>
        /// Gets the text value of a text box
        /// </summary>
        public string textbox_get_text(String xpath)
        {
            Ranorex.Text el = new Ranorex.Text(_app.GetElement(xpath));
            return el.TextValue;
        }
        
        /// <summary>
        /// Sets the text value of the text box directly
        /// </summary>
        public void textbox_set_text(String xpath, String value)
        {
        	if (String.IsNullOrEmpty(value))
        	{
        		textbox_clear_text(xpath);
        		return;
        	}
        	Ranorex.Text el = new Ranorex.Text(_app.GetElement(xpath));
        	if (!el.Enabled) throw new KeywordException("Text box is not enabled");
        	el.TextValue = value;
        }

        /// <summary>
        /// Clears the text of the text box directly
        /// </summary>
        public void textbox_clear_text(String xpath)
        {
        	Ranorex.Text el = new Ranorex.Text(_app.GetElement(xpath));
        	if (!el.Enabled) throw new KeywordException("Text box is not enabled");
        	el.TextValue = String.Empty;
        }
        
        #endregion

        #region Keyboard
        
        /// <summary>
        /// Sends the specified key sequence to the currently focussed control
        /// </summary>
        /// <param name="sequence"></param>
        public void keyboard_press(String sequence)
        {
        	Ranorex.Keyboard.Press(sequence);
        }
        
       
        #endregion
        
        #region Checkbox
        
		
        
        
        #endregion
        
        
    }
}
